<template>
	<view class="content">
		<view class="top-block">
			<view class="title-item">
				<view class="title">全部行业</view>
				<view class="title"><text style="color: #FF7626;">{{selectList.length}}</text>/{{max}}</view>
			</view>
			<view class="total-block" v-if="selectList.length>0">
				<view class="total">已选</view>
				<view class="total-show">
					<view class="box">
						<view class="total-item" :key="item.id" v-for="(item,index) in selectList">{{item.jobTitle}}
							<view class="iconfont icon-guanbi" @click="deteteItem(index)"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="category">
			<view class="second-category-block ">
				<view class="first-category-block ">
					<view :key="item.id" v-for="(item,index) in category" :class="menuActive==item.id?'active':''"
						class="item-list-block">
						<view class="title1">{{item.jobTitle}}</view>
						<view class="item-list-block" :key="item1.id" v-for="(item1,index) in item.subLevelModelList">
							<view class="title">{{item1.jobTitle}}</view>
							<view class="item-block">
								<view :key="item2.id" v-for="(item2,index2) in item1.subLevelModelList"
									:class="selectList.includes(item2)?'active':''" @click="changeMenuSelected(item2)"
									class="menu-item">{{item2.jobTitle}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btn-block">
			<view class="btn-content">
				<view class="btn cancel" @click="goBack">清除</view>
				<view class="btn confirm" @click="handleSave">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getJobCategory
	} from '@/api/config.js';
	import {
		multipleChoiceChangeObj,
		multipleChoiceChange
	} from '@/util/util.js'
	export default {
		data() {
			return {
				category: [],
				currentChild: null,
				menuActive: null,
				selectedObj: {},
				max: 3,
				selectIds: [],
				selectList: []
			}
		},
		methods: {
			changeMenuSelected(item) {
				multipleChoiceChange(this.selectList, item, 3)
			},
			goBack() {
				// uni.navigateBack()
				this.selectList = []
			},
			handleSave() {
				let copy = JSON.parse(JSON.stringify(this.selectList));
				if (copy.length === 0) {
					this.showToast('请至少选择一个！');
				} else {
					uni.$emit('getJobCategory', copy);
					uni.navigateBack()
				}
			},
			deteteItem(index) {
				var that = this;
				that.selectList.splice(index, 1)
			},
			findJobName() {
				uni.showLoading({
					title: '加载中'
				})
				getJobCategory({}).then((res) => {
					this.category = res;
					this.menuActive = this.category[0].id
					this.currentChild = this.category[0].subLevelModelList
					if (this.selectIds.length > 0) {
						this.getSelect()
					}
				}).catch(() => {
					this.showToast('加载失败', () => {
						uni.navigateBack()
					});
				})
			},
			getSelect() {
				for (let i = 0; i < this.selectIds.length; i++) {
					const res = this.mapTree(this.category, this.selectIds[i])
					if (res) {
						this.$set(this.selectedObj, res.id, res);
					}
				}
			},
			mapTree(nodes, id) {
				for (let _i = 0; _i < nodes.length; _i++) {
					if (nodes[_i].id == id) {
						return nodes[_i]
					} else {
						if (nodes[_i].subLevelModelList && nodes[_i].subLevelModelList.length > 0) {
							let res = this.mapTree(nodes[_i].subLevelModelList, id);
							if (res) {
								return res
							}
						}
					}
				}
				return null
			}
		},
		onLoad(e) {
			this.findJobName();
			this.max = e.max;
			if (e.id) {
				this.selectIds = e.id.split(',').filter((item) => {
					return item != 0
				});
			}
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 30rpx;

		.top-block {
			width: 100%;
			position: fixed;
			background-color: #F8F8F8;
			left: 0%;
			top: 0;
			padding: 30rpx;
			text-align: center;

			.title-item {
				display: flex;
				flex-direction: row;
				justify-content: space-between;

				.title {
					font-size: 24px;
					font-weight: bold;
				}
			}

			.total-block {
				display: flex;
				align-items: center;
				flex-direction: row;
				margin-top: 20rpx;

				.total {
					width: 160rpx;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
				}

				.total-show {
					width: calc(100% - 160rpx);
					height: 100%;
					overflow-x: scroll;
					padding: 0 10rpx;
					display: flex;
					align-items: center;

					.box {
						display: flex;
						flex-direction: row;
						align-items: center;
					}

					.total-item {
						border: 1px solid #FF7626;
						color: #FF7626;
						font-size: 26rpx;
						margin-right: 20rpx;
						padding: 6rpx 20rpx;
						border-radius: 10rpx;
						white-space: nowrap;
						display: flex;
						flex-direction: row;

						.iconfont {
							font-size: 10px;
							margin-left: 4rpx;
						}
					}
				}
			}
		}

		.category {
			width: 100%;
			display: flex;
			flex-direction: row;
			justify-content: center;
			margin-top: 100rpx;
			margin-bottom: 100rpx;

			.second-category-block {
				width: 100%;

				.item-list-block {
					width: 100%;
					display: flex;
					flex-direction: column;
					margin-bottom: 20rpx;

					.title {
						font-weight: bold;
						padding: 0 20rpx;
						margin-bottom: 20rpx;
						font-size: 15px;
					}

					.title1 {
						font-size: 19px;
						font-weight: bold;
						margin-bottom: 30rpx;
						margin-top: 60rpx;
						color: #FF7626;
					}

					.item-block {
						width: 100%;
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;
						//padding: 0 10rpx;

						.menu-item {
							padding: 12rpx 30rpx;
							width: calc((100% - 60rpx) / 3);
							color: #313131;
							font-size: 26rpx;
							margin: 10rpx;
							background: #efefef;
							border-radius: 10rpx;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
							text-align: center;
							display: flex;
							justify-content: center;
							//border: 1px solid #F6F6F6;
						}

						.menu-item:nth-child(3n) {
							margin-right: 0;
						}



						.menu-item.active {
							background: #FDECE5;
							color: #FE7526;
							//border: 1px solid #FF7626;
						}
					}

				}
			}
		}

		.btn-block {
			box-shadow: 0px -5px 20px 0 #efefef;
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 150rpx;
			display: flex;
			flex-direction: column;
			flex-wrap: wrap;
			justify-content: center;
			align-items: center;
			padding: 20rpx;
			background-color: #fff;
			left: 0;

			.btn-content {
				width: 100%;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: left;
				height: 100rpx;
				padding: 15rpx 0;
				bottom: 0;

				.btn {
					width: calc(50% - 20rpx);
					border-radius: 20rpx;
					font-size: 30rpx;
					background: #F6F6F6;
					color: #313131;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
				}

				.btn.cancel {
					width: calc(50% - 20rpx);
					margin-right: 40rpx;
				}

				.btn.confirm {
					color: #FF7626;
				}
			}
		}
	}
</style>